package cn.tristenstudy.blog.controller.admin;

import cn.tristenstudy.blog.exception.MyNotFoundException;
import cn.tristenstudy.blog.pojo.Type;
import cn.tristenstudy.blog.service.TypeService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * 分类后台管理
 *
 * @author xichen1.li
 * @date 2020-12-28 11:31
 */
@Controller
@RequestMapping("/admin/type")
public class TypeManagerController {

    @Autowired
    private TypeService typeService;

    /**
     * 分页查询
     *
     * @param current 分页条件
     * @param model   model对象
     * @return 页面
     */
    @GetMapping("/{current}/list")
    public String list(@PathVariable Long current, Model model) {
        if (current <= 0) {
            throw new MyNotFoundException("无法找到页面！");
        }
        Page<Type> result = typeService.listType(current);
        model.addAttribute("page", result);
        model.addAttribute("first", result.hasPrevious());
        model.addAttribute("last", result.hasNext());
        return "admin/types";
    }

    /**
     * 跳转至新增页面
     */
    @GetMapping("/input")
    public String input(Model model) {
        model.addAttribute("type", new Type());
        return "admin/types-input";
    }

    /**
     * 跳转至修改页面
     */
    @GetMapping("/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        Type type = typeService.getType(id);
        model.addAttribute("type", type);
        return "admin/types-input";
    }

    /**
     * 新增分类
     *
     * @param type       分类
     * @param attributes attributes
     */
    @PostMapping("/add")
    public String add(Type type, RedirectAttributes attributes) {
        boolean exist = typeService.isNotExist(type);
        if (!exist) {
            attributes.addFlashAttribute("msg", "该分类名存在！");
            return "redirect:/admin/type/input";
        }
        Boolean temp = typeService.saveType(type);
        if (temp) {
            attributes.addFlashAttribute("msg", "新增成功！");
        } else {
            attributes.addFlashAttribute("msg", "新增失败！");
        }
        return "redirect:/admin/type/1/list";
    }

    /**
     * 修改分类
     *
     * @param type       分类
     * @param attributes attributes
     */
    @PostMapping("/edit")
    public String edit(Type type, RedirectAttributes attributes) {
        boolean exist = typeService.isNotExist(type);
        if (!exist) {
            attributes.addFlashAttribute("msg", "该分类已存在！");
            return "redirect:/admin/type/" + type.getId() + "/input";
        }
        Boolean temp = typeService.updateType(type);
        if (temp) {
            attributes.addFlashAttribute("msg", "更新成功！");
        } else {
            attributes.addFlashAttribute("msg", "更新失败！");
        }
        return "redirect:/admin/type/1/list";
    }

    /**
     * 删除指定分类
     *
     * @param id         id
     * @param attributes attributes
     */
    @GetMapping("/{id}/{current}/delete")
    public String delete(@PathVariable Long id, @PathVariable int current, RedirectAttributes attributes) {
        Boolean result = typeService.deleteType(id);
        if (result) {
            attributes.addFlashAttribute("msg", "删除成功！");
            return "redirect:/admin/type/" + current + "/list";
        } else {
            attributes.addFlashAttribute("msg", "删除失败！");
            return "redirect:/admin/type/" + current + "/list";
        }
    }


}
